﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ClassLibrary.DAO;
using ClassLibrary.Entity;
namespace mprsc
{
    public partial class LoanPhone : Form
    {
        TblLoan2PhoneDAO loan2PhoneDAO = new TblLoan2PhoneDAO(Global.CONNECTION_STRING);

        public LoanPhone()
        {
            InitializeComponent();
        }

        private void addNewLoanPhone_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            AddNewLoanPhone addNewLoanPhone = new AddNewLoanPhone();
            addNewLoanPhone.Show();
            this.Hide();
        }
        private void loadLoanPhone()
        {
            try
            {
                grdLoanPhone.DataSource = loan2PhoneDAO.GetAll();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void LoanPhone_Load(object sender, EventArgs e)
        {
            this.CenterToScreen();
            loadLoanPhone();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            bool flag = false;
            if (MessageBox.Show("Do you want to delete this Loan Phone ?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                string id = grdLoanPhone.CurrentRow.Cells["LPhoneID"].Value.ToString();
                try
                {
                    TblLoan2Phone Lphone = new TblLoan2Phone();
                    string IDDelete = id;
                    loan2PhoneDAO.DeleteLPhone(IDDelete);
                    flag = true;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            if (flag)
            {
                loadLoanPhone();
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            bool flag = false;
            string id = grdLoanPhone.CurrentRow.Cells["LPhoneID"].Value.ToString();
            if (MessageBox.Show("Do you want to update Status this Loan Phone ?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    try
                    {
                        if (grdLoanPhone.CurrentRow.Cells["LoanStatus"].Value.ToString() == "Not Loan")
                        {
                            loan2PhoneDAO.Update(1, id);
                            loadLoanPhone();
                        }
                        else
                        {
                            loan2PhoneDAO.Update(0, id);
                            loadLoanPhone();
                        }
                        flag = true;
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }
            if (flag)
            {
                loadLoanPhone();
            }
        }
    }
}
